import 'package:flutter/material.dart';

enum HabitFrequency { daily, weekdays, weekends, custom }

class Habit {
  final String id;
  final String title;
  final IconData icon;
  final Color color;
  final TimeOfDay reminderTime;
  final HabitFrequency frequency;
  final List<int> customDays; // 对于自定义频率，0=周一，6=周日
  final DateTime createdAt;
  final String description;
  final List<DateTime> completedDates;
  
  Habit({
    required this.id,
    required this.title,
    required this.icon,
    this.color = Colors.green,
    required this.reminderTime,
    this.frequency = HabitFrequency.daily,
    this.customDays = const [],
    required this.createdAt,
    this.description = '',
    this.completedDates = const [],
  });

  // 计算当前连续天数
  int get currentStreak {
    if (completedDates.isEmpty) return 0;
    return 1; // 简化实现
  }
  
  // 今天是否已完成
  bool isCompletedToday() {
    final today = DateTime.now();
    return completedDates.any((date) => 
      date.year == today.year && 
      date.month == today.month && 
      date.day == today.day
    );
  }

  // 计算完成率
  double getCompletionRate(DateTime startDate, DateTime endDate) {
    int totalDays = endDate.difference(startDate).inDays + 1;
    int completedCount = 0;
    
    for (DateTime date = startDate; 
         !date.isAfter(endDate); 
         date = date.add(Duration(days: 1))) {
      if (isCompletedOn(date)) {
        completedCount++;
      }
    }
    
    return totalDays > 0 ? completedCount / totalDays : 0;
  }

  // 是否在特定日期完成
  bool isCompletedOn(DateTime date) {
    return completedDates.any((d) => 
      d.year == date.year && 
      d.month == date.month && 
      d.day == date.day
    );
  }
}
